/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taller.mongo;

import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alex
 */
public class mongo  {
     public mongo() {
        
        DB db;
        //DBCollection tabla, empleado, factura, producto, proveedor;
        DBCollection col = null;
        try {
            Mongo m = new Mongo("localhost", 27017);
            System.out.println("DBMS Mongo Conectado");
            db = m.getDB("test");
            col=db.getCollection("collection name");
            //base=m.getDB("proyectoG");
            col=db.getCollection("cliente");
            
            System.out.println("Base de Datos Mongo Conectada");
            
        } catch (UnknownHostException ex) {
            Logger.getLogger(mongo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MongoException ex) {
            Logger.getLogger(mongo.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    BasicDBObject student = new BasicDBObject();
    student.put("name", "rstname0, lastname0");
    student.put("age", 20);
    student.put("birthplace", "acapulco, MX");
    student.put("ID", 6782499);
    //se crea el objeto parents que contendra a su vez a father y mother
    BasicDBObject parents = new BasicDBObject();
    //se crea el objeto parents que contendra a su vez a father y mother
   
    //se crea el objeto father con sus respectivos datos
    BasicDBObject father = new BasicDBObject();
    father.put("name", "rstname1, lastname1");
    father.put("age", 46);
    father.put("birthplace", "hong kong, CN");
    father.put("ID", 91235);
    //se crea el objeto mother con sus respectivos datos
    BasicDBObject mother = new BasicDBObject();
    mother.put("name", "rstname2, lastname2");
    mother.put("age", 37);
    mother.put("birthplace", "Hanoi, VN");
    /*
    * por defecto al insertar un numero intentara insertarlo como integer
    * para insertar un long se puede utilizar la sigiente sintaxis.
    */
    mother.put("ID", new Long("062850957521"));
    //se agregan mother y father al objeto parents
    parents.put("father", father);
    parents.put("mother", mother);
    //se agrega parents al objeto student
    student.put("parents", parents);
    //se inserta el estudiante a la coleccion
    col.insert(student);
    //imprimimos los datos recorriendo la base de datos utilizando un cursor
    DBCursor cur = col.find();
    while(cur.hasNext()) 
    System.out.println(cur.next());
    

     }
}
